package com.example.leo.mycharmingstudent.a0902;

import android.support.annotation.Px;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;

import java.util.ArrayList;

public class FragmentPageAdapterActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private LinearLayout llTab;
    private TextView tvMainPage;
    private TextView tvMine;

    private ArrayList<MyFragment> mFragments = new ArrayList<>();
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_page_adapter);

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        mViewPager = findViewById(R.id.vp_fragment_activity_fragment_page_adapter);
        llTab = findViewById(R.id.ll_tab_activity_fragment_page_adapter);
        tvMainPage = findViewById(R.id.tv_main_page_activity_fragment_page_adapter);
        tvMine = findViewById(R.id.tv_mine_activity_fragment_page_adapter);
    }

    private void initData() {
        mFragments.add(MyFragment.newInstance("主页"));
        mFragments.add(MyFragment.newInstance("我的"));

        tvMainPage.setText("选中了");
    }

    private void initEvent() {
        mAdapter = new MyAdapter("Very good.",getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, @Px int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    tvMainPage.setText("选中了");
                    tvMine.setText("我的");
                }else if(position == 1){
                    tvMainPage.setText("首页");
                    tvMine.setText("选中了");
                }else {}
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        tvMainPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(0);
            }
        });
        tvMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(1);
            }
        });
    }

    class MyAdapter extends FragmentStatePagerAdapter{

        public MyAdapter(String name,FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get(i);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
